﻿using System.Data;
using System.Linq;
using MC.RideEvent.Mvc.Models.Entity;
using System;
using MC.RideEvent.Mvc.Models.ViewModel;
using AutoMapper;

namespace MC.RideEvent.Mvc.Models.Repository
{
    public class MemberRepository : IMemberRepository
    {
        private RideEventDBEntities _context;

        public MemberRepository()
        {
            _context = new RideEventDBEntities();
        }

        public bool SaveMember(MemberViewModel member, bool forUpdate)
        {
            Member m;
            bool result = false;

            if (forUpdate)
            {
                m = UpdateInstanceMember(member);

                if (m != null)
                {
                    _context.Entry(m);
                    _context.Entry(m).State = EntityState.Modified;
                    result = true;
                }
            }
            else
            {
                m = CreateInstanceMember(member);
                _context.Members.Add(m);
                result = true;
            }

            _context.SaveChanges();

            return result;
        }

        public MemberViewModel GetNewMember(string codeUser)
        {
            return null;
        }

        public bool ActivateNewMember(MemberViewModel user)
        {
            return false;
        }

        private Member CreateInstanceMember(MemberViewModel member)
        {
            var newMember = new Member();
            newMember.Code = Guid.NewGuid().ToString();
            newMember.Name = member.Name;
            newMember.FirstName = member.FirstName;
            newMember.Email = member.Email;
            newMember.Pwd = member.Password;
            newMember.Birthday = member.Birthday;
            newMember.Address = member.Address;
            newMember.City = member.City;
            newMember.Cp = member.Cp;
            newMember.Sexe = member.Sexe;
            newMember.DriveLicenceDate = member.DriveLicenceDate;
            //var newMember = AutoMapper.Mapper.Map<MemberModel, Member>(member);
            var dateNow = DateTime.Now;
            newMember.DateCreation = dateNow;
            newMember.DateUpdate = dateNow;

            return newMember;
        }

        private Member UpdateInstanceMember(MemberViewModel user)
        {
            var userFound = (from m in _context.Members
                             where m.Id == user.Id
                             select m).FirstOrDefault();

            if (userFound != null)
            {
                //quel sont les champs a modifier
                userFound.Address = user.Address;
                userFound.Name = user.Name;
                userFound.FirstName = user.FirstName;
                userFound.DriveLicenceDate = user.DriveLicenceDate;
                userFound.DateUpdate = DateTime.Now;
            }

            return userFound;
        }

        /// <summary>
        /// This method get the logged current user.
        /// </summary>
        /// <param name="email"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public MemberViewModel MakeConnection(string email, string pwd)
        {
            var item = (from m in _context.Members
                        where m.Email == email
                        where m.Pwd == pwd
                        select m).FirstOrDefault();

            var data = Mapper.Map<Member, MemberViewModel>(item);

            return data;
        }
    }
}
